$(document).ready(function() {
    
    // 固定楼盘图片高度，高度是宽度的75%
    var bgbWidth = $(".similar-grid-block").width();
    $(".similar-grid-block").find("img").css("height", bgbWidth * 0.75);
    $(window).resize(function() {
        var bgbWidth = $(".similar-grid-block").width();
        $(".similar-grid-block").find("img").css("height", bgbWidth * 0.75);
    });

    var ul = $("ul.similar-list");
    var ul_back = $("ul.similar-list-back");
    // 向左
    $(".similar-last").click(function(){
        $("ul.similar-list").find('li').eq(0).appendTo(ul_back);
        ul_back.find('li').eq(0).appendTo(ul);
    });
    // 向右
    $(".similar-next").click(function(){
        $("ul.similar-list").find('li:last').prependTo(ul_back);
        ul_back.find('li:last').prependTo(ul);
    });
    // 没有备选楼盘时左右按钮隐藏
    if ($("ul.similar-list-back").find('li').length == 0){
        $(".similar-last").css({'visibility':'hidden'});
        $(".similar-next").css({'visibility':'hidden'});
    }

    // 变价通知
    $(".change-price").click(function(){
        $(".tanchu_mask").show();
        $(".tanchu_8").show();
    });

    // 优惠通知
    $(".discount-note").click(function(){
        $(".tanchu_mask").show();
        $(".tanchu_9").show();
    });

    // 获取楼盘地址
    $(".house-address").click(function(){
        $(".tanchu_mask").show();
        $(".tanchu_10").show();
    });

	 // 一键免费通话，弹出框根据输入号码框是否显示判断是mobile版还是pc版
//    $(".freecall").click(function(){
//        if ($('.number-check').css("display") == "none"){
//            $(".tanchu_3").show();
//        } else if ($('.number-check').css("display") == "inline") {
//        	// 不输手机号码placeholder变红
//        	if ($(".tanchu_1 .phone-input").val() == ""){
// 			$(".tanchu_1 .phone-input").addClass("empty-input");
// 		}
// 		// 手机号码不符合正则表达式变红
// 		if (!$(".tanchu_1 .phone-input").val().match(/^1[3|4|5|7|8]\d{9}$/)){
// 			$(".tanchu_1 .phone-input").addClass("red-input");
// 		}
//            $(".tanchu_mask").show();
//            $(".tanchu_6").show();
//        }
//    });

    // 输入手机号码，有输入动作，变红的样式取消
	// $(".tanchu_1 .phone-input").on("input", function(){
	// 	$(".tanchu_1 .phone-input").removeClass("empty-input");
	// 	$(".tanchu_1 .phone-input").removeClass("red-input");
	// });

    // 弹出框右上角的关闭小图标，统一关闭所有弹出框
    $(".tanchu").find(".close").bind('click',function(){
        $(".tanchu_mask").hide();
        $(".tanchu_1").hide();
        $(".tanchu_2").hide();
        $(".tanchu_3").hide();
        $(".tanchu_4").hide();
        $(".tanchu_6").hide();
        $(".tanchu_7").hide();
        $(".tanchu_8").hide();
        $(".tanchu_9").hide();
        $(".tanchu_10").hide();
        $(".tanchu_11").hide();
        $(".tanchu_12").hide();
    });

    // 发送验证码短信按钮，点击过后按钮变灰
    $(".sms-btn").click(function(){
        $(this).addClass("code-btn-disable");
    });

    // 弹出框确定按钮，点击后关闭弹出框
    $(".SubBtn_tc2").click(function(){
        $(".tanchu_mask").hide();
        $(".tanchu_2").hide();
    });

    if (isFang90com && isMobile) {
        // fang90 移动端下面直接显示底部功能块
        $('.callme').show();
    } else {
        // 免费咨询底部悬浮框，鼠标下滑时显示，鼠标上滑时隐藏
        var modularListsOffSetTop = $('.modular-lists').offset().top;
        $(document).resize(function() {
            modularListsOffSetTop = $('.modular-lists').offset().top;
        });
        $(window).bind('scroll', function() {
            if ($(window).scrollTop() > modularListsOffSetTop) {
                $('.callme').show();
            } else {
                $('.callme').hide();
            }
        });
    }
});

// photoswipe

var initPhotoSwipeFromDOM = function(gallerySelector) {

    // parse slide data (url, title, size ...) from DOM elements 
    // (children of gallerySelector)
    var parseThumbnailElements = function(el) {
        var thumbElements = el.childNodes,
            numNodes = thumbElements.length,
            items = [],
            figureEl,
            childElements,
            linkEl,
            size,
            item;

        for (var i = 0; i < numNodes; i++) {


            figureEl = thumbElements[i]; // <figure> element

            // include only element nodes 
            if (figureEl.nodeType !== 1) {
                continue;
            }

            linkEl = figureEl.children[0]; // <a> element

            size = linkEl.getAttribute('data-size').split('x');

            // create slide object
            item = {
                src: linkEl.getAttribute('href'),
                w: parseInt(size[0], 10),
                h: parseInt(size[1], 10)
            };



            if (figureEl.children.length > 1) {
                // <figcaption> content
                item.title = figureEl.children[1].innerHTML;
            }

            if (linkEl.children.length > 0) {
                // <img> thumbnail element, retrieving thumbnail url
                item.msrc = linkEl.children[0].getAttribute('src');
            }

            item.el = figureEl; // save link to element for getThumbBoundsFn
            items.push(item);
        }

        return items;
    };

    // find nearest parent element
    var closest = function closest(el, fn) {
        return el && (fn(el) ? el : closest(el.parentNode, fn));
    };

    // triggers when user clicks on thumbnail
    var onThumbnailsClick = function(e) {
        e = e || window.event;
        e.preventDefault ? e.preventDefault() : e.returnValue = false;

        var eTarget = e.target || e.srcElement;

        var clickedListItem = closest(eTarget, function(el) {
            return el.tagName === 'FIGURE';
        });

        if (!clickedListItem) {
            return;
        }


        // find index of clicked item
        var clickedGallery = clickedListItem.parentNode,
            childNodes = clickedListItem.parentNode.childNodes,
            numChildNodes = childNodes.length,
            nodeIndex = 0,
            index;

        for (var i = 0; i < numChildNodes; i++) {
            if (childNodes[i].nodeType !== 1) {
                continue;
            }

            if (childNodes[i] === clickedListItem) {
                index = nodeIndex;
                break;
            }
            nodeIndex++;
        }



        if (index >= 0) {
            openPhotoSwipe(index, clickedGallery);
        }
        return false;
    };

    // parse picture index and gallery index from URL (#&pid=1&gid=2)
    var photoswipeParseHash = function() {
        var hash = window.location.hash.substring(1),
            params = {};

        if (hash.length < 5) {
            return params;
        }

        var vars = hash.split('&');
        for (var i = 0; i < vars.length; i++) {
            if (!vars[i]) {
                continue;
            }
            var pair = vars[i].split('=');
            if (pair.length < 2) {
                continue;
            }
            params[pair[0]] = pair[1];
        }

        if (params.gid) {
            params.gid = parseInt(params.gid, 10);
        }

        if (!params.hasOwnProperty('pid')) {
            return params;
        }
        params.pid = parseInt(params.pid, 10);
        return params;
    };

    var openPhotoSwipe = function(index, galleryElement, disableAnimation) {
        var pswpElement = document.querySelectorAll('.pswp')[0],
            gallery,
            options,
            items;

        items = parseThumbnailElements(galleryElement);

        // define options (if needed)
        options = {
            index: index,

            // define gallery index (for URL)
            galleryUID: galleryElement.getAttribute('data-pswp-uid'),

            getThumbBoundsFn: function(index) {
                // See Options -> getThumbBoundsFn section of docs for more info
                var thumbnail = items[index].el.getElementsByTagName('img')[0], // find thumbnail
                    pageYScroll = window.pageYOffset || document.documentElement.scrollTop,
                    rect = thumbnail.getBoundingClientRect();

                return {
                    x: rect.left,
                    y: rect.top + pageYScroll,
                    w: rect.width
                };
            },

            // history & focus options are disabled on CodePen
            // remove these lines in real life: 
            historyEnabled: false,
            focus: false

        };

        if (disableAnimation) {
            options.showAnimationDuration = 0;
        }

        // Pass data to PhotoSwipe and initialize it
        gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, options);
        gallery.init();
    };

    // loop through all gallery elements and bind events
    var galleryElements = document.querySelectorAll(gallerySelector);

    for (var i = 0, l = galleryElements.length; i < l; i++) {
        galleryElements[i].setAttribute('data-pswp-uid', i + 1);
        galleryElements[i].onclick = onThumbnailsClick;
    }

    // Parse URL and open gallery if it contains #&pid=3&gid=1
    var hashData = photoswipeParseHash();
    if (hashData.pid > 0 && hashData.gid > 0) {
        openPhotoSwipe(hashData.pid - 1, galleryElements[hashData.gid - 1], true);
    }
};
initPhotoSwipeFromDOM('.my-simple-gallery');

$(document).ready(function() {
    $('.layout').on('click', function() {
        $('.my-simple-gallery').find('img')[0].click();
    });
});
